package com.sino.second.business.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.tempuri.SentClient;

import com.sino.second.bean.TblExpertmessage;
import com.sino.second.bean.TblMessage;
import com.sino.second.business.ISmsService;
import com.sino.second.dao.ISmsendDAO;
import com.sino.second.dao.hibernate.SmsDAO;
import com.sino.second.util.MobPhnNumMatch;
import com.sino.util.SysParameter;
import com.sino.util.page.PagerModel;

public class SmsServiceImpl implements ISmsService {
	private ISmsendDAO smsendDAO;

	private SmsDAO smsDAO;

	public PagerModel findExpertsSmsList(String hqlWhere) {
		// TODO Auto-generated method stub
		return this.findExpertsSmsList(hqlWhere);
	}

	// xyw 查看短信状态
	public List findExpertsSmsListByFBh(int FBh, String hqlWhere) {
		// TODO Auto-generated method stub
		if (null != this.getSmsDAO().findExpertsSmsListByFBh(FBh, hqlWhere)) {
			return this.getSmsDAO().findExpertsSmsListByFBh(FBh, hqlWhere);
		}
		return null;
	}

	// xyw給专家发信息
	public String saveExpertsSms(List smsList, String[] mobiles, String message) {
		List<TblExpertmessage> tmList1 = smsList;
		List<TblExpertmessage> tmList = new ArrayList<TblExpertmessage>();
		String mobileStr = "";
		for (int n = 0; n < mobiles.length; n++) {
			String mo = mobiles[n];
			String moid[] = mo.split("\\|");
			if (2 == moid.length) {
				// 判断是否为手机号码
				MobPhnNumMatch mobPhnNumMatch = new MobPhnNumMatch(moid[1]);
				if (1 == mobPhnNumMatch.matchNum()
						|| 2 == mobPhnNumMatch.matchNum()
						|| 3 == mobPhnNumMatch.matchNum()) {
					if (mobileStr == "") {
						mobileStr = moid[1];

					} else {
						mobileStr += "," + moid[1];
					}
					tmList1.get(n).setContent(message);
					tmList1.get(n).setIssend("1");
					tmList1.get(n).setSenddate(new Date());
					tmList1.get(n).setYear(SysParameter.CURRENT_YEAR);
					tmList.add(tmList1.get(n));
				} else {
					continue;
				}
			} else {
				continue;
			}
		}
		String flag = "";
		SentClient sc = new SentClient();
		String smsmark = "";
		try {
			 smsmark = sc.sent(mobileStr,message);
		} catch (Exception e) {
			flag = "0";
		}
		if (smsmark.startsWith("0%^&")) {
			if (null == tmList) {
				 System.out.println("空");
			} else {
				this.smsDAO.saveSmsCollection(tmList);
				flag = "2";
			}
		} else {
//			 this.smsDAO.saveSmsCollection(tmList);
			flag = "1";
		}
		return flag;
	}

	// xyw 项目发送信息查看 从信息表里取
	public List findXmSmsListByXmId(String zlid, String hqlWhere) {
		// TODO Auto-generated method stub
		return this.smsDAO.findXmSmsListByXmId(zlid, hqlWhere);
	}

	// 给项目发信息
	public String saveXmSms(List xmsList, String[] mobiles, String message) {
		List<TblMessage> tmList3 = xmsList;
		List<TblMessage> tmList2 = new ArrayList<TblMessage>();
		String mobileStr = "";
		for (int n = 0; n < mobiles.length; n++) {
			String mo = mobiles[n];
			String moid[] = mo.split("\\|");
			if (2 == moid.length) {
				// 判断是否为手机号码
				MobPhnNumMatch mobPhnNumMatch = new MobPhnNumMatch(moid[1]);
				if (1 == mobPhnNumMatch.matchNum()
						|| 2 == mobPhnNumMatch.matchNum()
						|| 3 == mobPhnNumMatch.matchNum()) {
					if (mobileStr == "") {
						mobileStr = moid[1];

					} else {
						mobileStr += "," + moid[1];
					}
					tmList3.get(n).setContent(message);
					tmList3.get(n).setIssend("1");
					tmList3.get(n).setSenddate(new Date());
					tmList3.get(n).setYear(SysParameter.CURRENT_YEAR);
					tmList2.add(tmList3.get(n));

				} else {
					continue;
				}

			} else {
				continue;
			}
		}
		String flag = "";
		SentClient sc = new SentClient();
		String smsmark = "";
		try {
			 smsmark = sc.sent(mobileStr,message);
		} catch (Exception e) {
			flag = "0";
		}
		if (smsmark.startsWith("0%^&")) {
			if (null == tmList2) {
				// System.out.println("空");
			} else {
				this.smsDAO.saveSmsCollection(tmList2);
				flag = "2";
			}
		} else {
			this.smsDAO.saveSmsCollection(tmList2);
			flag = "1";
		}
		return flag;
	}

	public PagerModel findXmSmsList(String hqlWhere) {
		// TODO Auto-generated method stub
		return this.getSmsendDAO().findXmSmsList(hqlWhere);
	}

	public void saveSmsCollection(Collection o) {
		this.getSmsendDAO().saveSmsCollection(o);

	}

	public ISmsendDAO getSmsendDAO() {
		return smsendDAO;
	}

	public void setSmsendDAO(ISmsendDAO smsendDAO) {
		this.smsendDAO = smsendDAO;
	}

	public SmsDAO getSmsDAO() {
		return smsDAO;
	}

	public void setSmsDAO(SmsDAO smsDAO) {
		this.smsDAO = smsDAO;
	}

}
